package com.service.business.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.business.member.service.MemberMiningOrderService;
import com.service.business.support.SessionUtil;
import com.service.core.rest.request.PageRequest;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.OrderReleaseAwardEnum;
import com.service.dao.mapper.MemberMiningOrderMapper;
import com.service.dao.model.MemberMiningOrder;
import com.service.dao.model.OrderProductHrCombo;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class MemberMiningOrderServiceImpl extends ServiceImpl<MemberMiningOrderMapper, MemberMiningOrder> implements MemberMiningOrderService {
	@Autowired
	private SessionUtil sessionUtil;

	/**
	 * 获取用户线性冻结记录列表
	 *
	 * @param pageReq
	 *  @param coinsName
	 * @return
	 */
	@Override
	public CommonPageRep<MemberMiningOrder> pageByMemberIdAndCoins(PageRequest pageReq, String coinsName) {
		Page<OrderProductHrCombo> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
		List<MemberMiningOrder> miningOrders = list(new QueryWrapper<MemberMiningOrder>().lambda()
				.eq(MemberMiningOrder::getMemberId, sessionUtil.getUser().getUserId())
				.eq(MemberMiningOrder::getCoinsName, coinsName)
				.eq(MemberMiningOrder::getIsFinish, 0)
				.orderByDesc(MemberMiningOrder::getCreateTime)
		);

		return CommonPageRep.buildResultRep(page,miningOrders);
	}

	/**
	 * 统计用户线性冻结总资产
	 *
	 * @param memberId
	 * @param coinsId
	 * @return
	 */
	@Override
	public BigDecimal totalMiningFrostNum(Integer memberId, Integer coinsId, Integer orderId, Date someDay) {
		String somedayStr = null;
		if(!ObjectUtils.isEmpty(someDay)){
			somedayStr = DateFormatUtils.format(someDay, DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.getPattern());
		};
		return baseMapper.totalMiningFrostNum(memberId, coinsId,orderId,somedayStr);
	}

	/**
	 * 统计用户线性冻结总释放
	 *
	 * @param memberId
	 * @param coinsId
	 * @return
	 */
	@Override
	public BigDecimal totalMiningReleaseNum(Integer memberId, Integer coinsId, Integer orderId,Date someDay) {
		String somedayStr = null;
		if(!ObjectUtils.isEmpty(someDay)){
			somedayStr = DateFormatUtils.format(someDay, DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.getPattern());
		};
		return baseMapper.totalMiningReleaseNum(memberId, coinsId,orderId,somedayStr);
	}

	/**
	 * @param memberId
	 * @param coinsId
	 * @Description: 统计当前用户还有多少待释放数量
	 * @Author: ty
	 * @Date: 2021/4/30 13:46
	 * @param: [memberId, coinsId]
	 * @return: java.math.BigDecimal
	 */
	@Override
	public BigDecimal totalWaitReleaseNum(Integer memberId, Integer coinsId) {
		return baseMapper.totalWaitReleaseNum(memberId, coinsId);
	}
}
